package com.example.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.domain.Category;
import com.example.domain.MovieCategory;
import com.example.mapper.MovieCategoryMapper;
import com.example.service.CategoryService;
import com.example.mapper.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author Administrator
* @description 针对表【category】的数据库操作Service实现
* @createDate 2023-03-20 09:44:10
*/
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService{

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private MovieCategoryMapper movieCategoryMapper;

    @Override
    public IPage<Category> getList(Page<Category> page) {
        return categoryMapper.selectPage(page,null);
    }

    @Override
    public Category getById(String id) {
        return categoryMapper.selectById(id);
    }

    @Override
    public boolean save(Category category) {
        return categoryMapper.insert(category) > 0;
    }

    @Override
    public boolean update(String id, Category category) {
        Category category1 = categoryMapper.selectById(id);
        if (category1 != null){
            category.setId(id);
            return categoryMapper.updateById(category) > 0;
        }
        return false;
    }

    @Override
    public boolean delete(String id) {
        List<MovieCategory> movieCategories = movieCategoryMapper.selectList(new LambdaQueryWrapper<MovieCategory>()
                .eq(MovieCategory::getCategoryId, id));
        if (CollectionUtil.isEmpty(movieCategories)){
            return categoryMapper.deleteById(id) > 0;
        }
        return false;
    }
}




